#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    static int totalFruit(vector<int>& fruits) {
        // int kinds = 0;
        // int hash[10000] = {0};
        unordered_map<int, int> hash;
        int ret = 0;
        for(int left = 0, right = 0; right < fruits.size(); right++)
        {
            //进窗口：判断hash[f[r]]++
            if(hash.find(fruits[right]) == hash.end())
            {
                hash.insert({fruits[right], 1});
            }
            else
            {
                hash[fruits[right]]++;
            }
            //判断：hash.size() > 2 ? left++ : 不用任何操作
            while(hash.size() > 2)
            {
                //假如此时hash[fruits[left]]为0了，说明种类少了一种，所以将这个位置清空
                if(!--hash[fruits[left]])
                    hash.erase(fruits[left]);
                left++;
            }
            ret = max(ret, right-left+1);//更新结果
        }
        return ret;
    }
};

int main()
{
    // vector<int> fruits = {1, 2, 1};
    // vector<int> fruits = {1, 2, 3, 2, 2};
    vector<int> fruits = {3,3,3,1,2,1,1,2,3,3,4};
    cout << Solution::totalFruit(fruits) << endl;
    return 0;
}